package cn.lili.modules.wallet.serviceimpl;


import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.lili.common.security.enums.SceneEnums;
import cn.lili.common.vo.PageVO;
import cn.lili.modules.member.entity.vo.MemberPointsHistoryVO;
import cn.lili.modules.payment.entity.dos.UserPointsHistory;
import cn.lili.modules.payment.entity.dos.UserWallet;
import cn.lili.modules.wallet.mapper.MemberPointsHistoryMapper;
import cn.lili.modules.wallet.service.UserPointsHistoryService;
import cn.lili.modules.wallet.service.WalletService;
import cn.lili.mybatis.util.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 用户积分历史业务层实现
 *
 * @author Bulbasaur
 * @since 2020-02-25 14:10:16
 */
@Service
@RequiredArgsConstructor
public class UserPointsHistoryServiceImpl extends ServiceImpl<MemberPointsHistoryMapper, UserPointsHistory> implements UserPointsHistoryService {


    private final WalletService walletService;

    @Override
    public MemberPointsHistoryVO pointsHistoryVO(String userId) {
        //获取用户积分历史
        UserWallet userWallet = walletService.getFromUserid(userId);
        MemberPointsHistoryVO memberPointsHistoryVO = new MemberPointsHistoryVO();
        if (ObjectUtil.isNull(userWallet)) {
            return memberPointsHistoryVO;
        }
        memberPointsHistoryVO.setPoints(userWallet.getPoints());
        memberPointsHistoryVO.setTotalPoint(userWallet.getTotalPoints());
        return memberPointsHistoryVO;
    }

    @Override
    public Page<UserPointsHistory> pointsHistoryList(PageVO page, String userId, String username) {
        LambdaQueryWrapper<UserPointsHistory> lambdaQueryWrapper = new LambdaQueryWrapper<UserPointsHistory>()
                .eq(CharSequenceUtil.isNotEmpty(userId), UserPointsHistory::getMemberId, userId)
                .like(CharSequenceUtil.isNotEmpty(username), UserPointsHistory::getMemberName, username);
        //如果排序为空，则默认创建时间倒序
        if (CharSequenceUtil.isEmpty(page.getSort())) {
            page.setSort("createTime");
            page.setOrder("desc");
        }
        return this.page(PageUtil.initPage(page), lambdaQueryWrapper);
    }

}